﻿using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using UltiCoreLibrary;

namespace UltiSheet
{
    public partial class PartyResult : Form
    {
        private GameState gameState;

        public GameState GameState
        {
            // get { return gameState; }
            set { gameState = value; }
        }

        public PartyResult()
        {
            InitializeComponent();
        }

        private Dictionary<CheckBox, BasicGame> gameCheckBoxes = new Dictionary<CheckBox, BasicGame>();

        private Dictionary<CheckBox, BasicGame> unbidGameCheckBoxes = new Dictionary<CheckBox, BasicGame>();

        private void PartyResult_Activated(object sender, EventArgs e)
        {
            Party party = gameState.parties.Last.Value;
            int i = 0;
            Boolean unbidUlti = true;
            Boolean unbid100 = true;
            Boolean unbidDurchmars = true;
            Boolean unbid4Ace = true;

            foreach (BasicGame basicGame in party.Game.SubGames.Keys)
            {
                CheckBox gameBox = new CheckBox();
                gameBox.Text = basicGame.ToString() + " " + basicGame.Value.ToString();
                gameBox.Location = new Point(0, 5 + i * 25);
                gameBox.Size = new Size(200, 20);
                i++;
                gameCheckBoxes.Add(gameBox, basicGame);
                gameBox.CheckStateChanged += new EventHandler(gameBox_CheckStateChanged);
                gamesPanel1.Controls.Add(gameBox);
                if (
                    basicGame.Game == BasicGameType.GAME_ULTI ||
                    basicGame.Game == BasicGameType.GAME_BETLI )
                {
                    unbidUlti = false;
                }

                if (
                    basicGame.Game == BasicGameType.GAME_20 ||
                    basicGame.Game == BasicGameType.GAME_40 ||
                    basicGame.Game == BasicGameType.GAME_BETLI )
                {
                    unbid100 = false;
                }
                if (
                    basicGame.Game == BasicGameType.GAME_DURCHMARS ||
                    basicGame.Game == BasicGameType.GAME_4ACE ||
                    basicGame.Game == BasicGameType.GAME_BETLI )
                {
                    unbid4Ace = false;
                    unbidDurchmars = false;
                }
            }

            if (party.Game.Trump.Color != GameColors.COLOR_COLORLESS)
            {
                foreach (BasicGame basicGame in BasicGame.unBidGames)
                {
                    if (
                        (party.Game.Trump.Color == GameColors.COLOR_HEARTS && basicGame.Hearts) ||
                        (party.Game.Trump.Color != GameColors.COLOR_HEARTS && !basicGame.Hearts)
                        )
                    {
                        if (
                            (basicGame.Game == BasicGameType.GAME_100 && !unbid100) ||
                            (basicGame.Game == BasicGameType.GAME_ULTI && !unbidUlti) ||
                            (basicGame.Game == BasicGameType.GAME_FAILED_ULTI && !unbidUlti) ||
                            (basicGame.Game == BasicGameType.GAME_DURCHMARS && !unbidDurchmars) ||
                            (basicGame.Game == BasicGameType.GAME_4ACE && !unbid4Ace)
                            )
                        {
                            continue;
                        }
                        CheckBox gameBox = new CheckBox();
                        gameBox.Text = basicGame.ToString() + " " + basicGame.Value.ToString();
                        gameBox.Location = new Point(0, 5 + i * 25);
                        gameBox.Size = new Size(200, 20);
                        i++;
                        unbidGameCheckBoxes.Add(gameBox, basicGame);
                        gamesPanel1.Controls.Add(gameBox);
                    }
                }
            }
        }

        void gameBox_CheckStateChanged(object sender, EventArgs e)
        {
            CheckBox gameBox = (CheckBox)sender;
            BasicGame subGame = gameCheckBoxes[gameBox];

        }

        private void PartyResult_Closing(object sender, CancelEventArgs e)
        {
            Party party = gameState.parties.Last.Value;
            Game game = party.Game;
            if (game.Trump.Color == GameColors.COLOR_COLORLESS)
            {
                foreach (CheckBox box in gameCheckBoxes.Keys)
                {
                    if (box.Checked)
                    {
                        party.pointPrize =
                            +gameCheckBoxes[box].Value * party.ContraDefender1.Value
                            + gameCheckBoxes[box].Value * party.ContraDefender2.Value;
                        party.defender1Prize = -gameCheckBoxes[box].Value * party.ContraDefender1.Value;
                        party.defender2Prize = -gameCheckBoxes[box].Value * party.ContraDefender2.Value;
                        party.partyResult += gameCheckBoxes[box].Name + " ";

                        gameCheckBoxes[box].Success = true;

                    }
                    else
                    {
                        party.pointPrize =
                            -gameCheckBoxes[box].Value * party.ContraDefender1.Value
                            - gameCheckBoxes[box].Value * party.ContraDefender2.Value;
                        party.defender1Prize = gameCheckBoxes[box].Value * party.ContraDefender1.Value;
                        party.defender2Prize = gameCheckBoxes[box].Value * party.ContraDefender2.Value;
                        party.partyResult += "Failed " + gameCheckBoxes[box].Name + " ";
                        gameCheckBoxes[box].Success = false;
                    }
                }
            }
            else
            {
                foreach (CheckBox box in gameCheckBoxes.Keys)
                {
                    if (box.Checked)
                    {
                        party.pointPrize += gameCheckBoxes[box].Value * game.SubGames[gameCheckBoxes[box]].Value * 2;
                        party.defender1Prize -= gameCheckBoxes[box].Value * game.SubGames[gameCheckBoxes[box]].Value;
                        party.defender2Prize -= gameCheckBoxes[box].Value * game.SubGames[gameCheckBoxes[box]].Value;
                        if (game.SubGames[gameCheckBoxes[box]].Value != 1)
                        {
                            party.partyResult += game.SubGames[gameCheckBoxes[box]].name + " ";
                        }
                        party.partyResult += gameCheckBoxes[box].Name + " ";
                        gameCheckBoxes[box].Success = true;
                    }
                    else
                    {
                        party.pointPrize -= gameCheckBoxes[box].Value * game.SubGames[gameCheckBoxes[box]].Value * 2;
                        party.defender1Prize += gameCheckBoxes[box].Value * game.SubGames[gameCheckBoxes[box]].Value;
                        party.defender2Prize += gameCheckBoxes[box].Value * game.SubGames[gameCheckBoxes[box]].Value;
                        if (game.SubGames[gameCheckBoxes[box]].Value != 1)
                        {
                            party.partyResult += game.SubGames[gameCheckBoxes[box]].name + " ";
                        }
                        party.partyResult += "Failed " + gameCheckBoxes[box].Name + " ";
                        gameCheckBoxes[box].Success = false;
                    }
                }
                foreach (CheckBox box in unbidGameCheckBoxes.Keys)
                {
                    // this calculation is not valid for unbid games
                    if (box.Checked)
                    {
                        party.pointPrize += unbidGameCheckBoxes[box].Value* 2;
                        party.defender1Prize -= unbidGameCheckBoxes[box].Value;
                        party.defender2Prize -= unbidGameCheckBoxes[box].Value;
                        party.partyResult += "Unbid " + unbidGameCheckBoxes[box].Name + " ";

                        unbidGameCheckBoxes[box].Success = true;
                        party.Game.SubGames.Add(unbidGameCheckBoxes[box], new ContraLevel(ContraLevels.NORMAL));
                    }
                        /*
                    else
                    {
                        party.pointPrize -= unbidGameCheckBoxes[box].Value * 2;
                        party.defender1Prize += unbidGameCheckBoxes[box].Value;
                        party.defender2Prize += unbidGameCheckBoxes[box].Value;
                    }
                         */
                }
            }
        }
    }
}
